[bits 32]

global asm_hello_world_my_id
global asm_lidt
global asm_unhandled_interrupt
global asm_halt
global asm_int32
ASM_UNHANDLED_INTERRUPT_INFO db 'Unhandled interrupt happened, halt...'
                             db 0
ASM_IDTR dw 0
         dd 0

; void asm_unhandled_interrupt()
asm_unhandled_interrupt:
    cli
    mov esi, ASM_UNHANDLED_INTERRUPT_INFO
    xor ebx, ebx
    mov ah, 0x03
.output_information:
    cmp byte[esi], 0
    je .end
    mov al, byte[esi]
    mov word[gs:bx], ax
    inc esi
    add ebx, 2
    jmp .output_information
.end:
    jmp $

; void asm_lidt(uint32 start, uint16 limit)
asm_lidt:
    push ebp
    mov ebp, esp
    push eax

    mov eax, [ebp + 4 * 3]
    mov [ASM_IDTR], ax
    mov eax, [ebp + 4 * 2]
    mov [ASM_IDTR + 2], eax
    lidt [ASM_IDTR]

    pop eax
    pop ebp
    ret

asm_int32:
    int 32
    ret

asm_hello_world_my_id:
    push eax
    xor eax, eax

    mov ah, 0x05
    mov al, '2'
    mov word[gs:2 * (80 * 1 + 0)], ax   ; 第二行顶格输出学号名字

    mov al, '2'
    mov word[gs:2 * (80 * 1 + 1)], ax

    mov al, '3'
    mov word[gs:2 * (80 * 1 + 2)], ax

    mov al, '2'
    mov word[gs:2 * (80 * 1 + 3)], ax

    mov al, '0'
    mov word[gs:2 * (80 * 1 + 4)], ax

    mov al, '1'
    mov word[gs:2 * (80 * 1 + 5)], ax

    mov al, '3'
    mov word[gs:2 * (80 * 1 + 6)], ax

    mov al, '1'
    mov word[gs:2 * (80 * 1 + 7)], ax

    mov al, 'W'
    mov word[gs:2 * (80 * 1 + 8)], ax

    mov al, 'e'
    mov word[gs:2 * (80 * 1 + 9)], ax

    mov al, 'i'
    mov word[gs:2 * (80 * 1 + 10)], ax

    mov al, ' '
    mov word[gs:2 * (80 * 1 + 11)], ax

    mov al, 'B'
    mov word[gs:2 * (80 * 1 + 12)], ax

    mov al, 'a'
    mov word[gs:2 * (80 * 1 + 13)], ax

    mov al, 'i'
    mov word[gs:2 * (80 * 1 + 14)], ax

    mov al, 'Q'
    mov word[gs:2 * (80 * 1 + 15)], ax

    mov al, 'i'
    mov word[gs:2 * (80 * 1 + 16)], ax

    mov al, 'a'
    mov word[gs:2 * (80 * 1 + 17)], ax

    mov al, 'n'
    mov word[gs:2 * (80 * 1 + 18)], ax

    mov al, 'g'
    mov word[gs:2 * (80 * 1 + 19)], ax

    pop eax
    ret

asm_halt:
    jmp $